# coding=utf-8
# 最小二乘法


import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets,linear_model
from sklearn.metrics import mean_absolute_error,mean_squared_error,r2_score

diabetes = datasets.load_diabetes()


diabetes_X = diabetes.data[:,np.newaxis,2]

diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]

diabetes_y_train = diabetes.target[:-20]
diabetes_y_test  = diabetes.target[-20:]

regr = linear_model.LinearRegression()

regr.fit(diabetes_X_train,diabetes_y_train)

diabetes_y_pred = regr.predict(diabetes_X_test)


print('Coefficients: \t',regr.coef_)
print mean_squared_error(diabetes_y_test,diabetes_X_test)

print ('Mean squared error:%.2f'%mean_squared_error(diabetes_y_test,diabetes_X_test))

print ('Variance score: %.2f'%r2_score(diabetes_y_test,diabetes_X_test))

plt.scatter(diabetes_X_test,diabetes_y_test,color='black')

plt.plot(diabetes_X_test,diabetes_y_pred,color='blue',linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show()
